var txcd = {
    setBtnStatus: function () {
        var row = $('#grid_txcd').datagrid('getSelected');
        if (row && row.id) {
                //未导入
                if (row.czzt == 'wdr') {
                    $("#btn_txcd_insert").show();//导入
                    $("#btn_txcd_cx").hide();//撤销
                    $("#btn_txcd_cz").hide();//重置
                    $("#btn_txcd_qx").show();//取消
                }
                if (row.czzt == 'drcg') {
                    $("#btn_txcd_insert").hide();//导入
                    $("#btn_txcd_cx").show();//撤销
                    $("#btn_txcd_cz").hide();//重置
                    $("#btn_txcd_qx").hide();//取消
                }
                if (row.czzt == 'drsb') {
                    $("#btn_txcd_insert").hide();//导入
                    $("#btn_txcd_cx").hide();//撤销
                    $("#btn_txcd_cz").show();//重置
                    $("#btn_txcd_qx").hide();//取消
                }
                if (row.czzt == 'sccg') {
                    $("#btn_txcd_insert").show();//导入
                    $("#btn_txcd_cx").hide();//撤销
                    $("#btn_txcd_cz").hide();//重置
                    $("#btn_txcd_qx").show();//取消
                }
                if (row.czzt == 'scsb') {
                    $("#btn_txcd_insert").hide();//导入
                    $("#btn_txcd_cx").hide();//撤销
                    $("#btn_txcd_cz").show();//重置
                    $("#btn_txcd_qx").hide();//取消
                }
        } else {
            $("#btn_txcd_insert").hide();//导入
            $("#btn_txcd_cx").hide();//撤销
            $("#btn_txcd_cz").hide();//重置
            $("#btn_txcd_qx").hide();//取消
        }
    }
}
$(function () {
    //表格初始加载
    $('#grid_txcd').datagrid({
        url:$.webPath() + "/cwgl/txcd/txcdList",
        toolbar:"#toolbar_txcd",
        fitColumns:true,
        singleSelect:true,
        pagination:true,
        fit:true,
        pageNumber: 1,
        rownumbers:true,
        striped:true,
        columns:[[
            {field:'id', title:'单据号'},
            {field:'nbgs_jc', title:'公司名称', width:30,align: 'center'},
            {field:'status', title:'凭证类型', width:15,align: 'center',
                formatter:function (value) {
                    if (value == '2')
                        return '票据贴现';
                    if (value == '3')
                        return '票据托收';
                    if (value == '4')
                        return '票据承兑';
                }
            },
            {field:'pjlx', title:'票据类型', width:15,align: 'center',
                formatter:function (value) {
                    if (value == '0')
                        return '银票';
                    if (value == '1')
                        return '商票';
                }
            },
            {field:'rzyh_qc', title:'入账银行', width:15,align: 'center'},
            {field:'dqje', title:'票面金额(元)', width:15,align: 'right',
                formatter:function(value){
                    var sl = formatJe2(value,",");
                    return sl;
                }
            },
            {field:'createUser_xm', title:'制单人', width:15,align: 'center'},
            {field:'dateCreated', title:'制单日期', width:30,align: 'center'},
            {field:'voucherNum', title:'凭证号', width:15,align: 'center'},
            {field:'bz', title:'备注', width:20,align: 'center'},
            {field:'pjhm', title:'票据号码', width:30,align: 'center'},
            {field: 'czzt', title: '状态', align: 'center',width: 10, formatter: function (value, row, index) {
                if (value == 'wdr')
                    return "未导入";
                if (value == 'drcg')
                    return "导入成功";
                if (value == 'drsb')
                    return "导入失败";
                if (value == 'sccg')
                    return "删除成功";
                if (value == 'scsb')
                    return "删除失败";
                return "";
            }}
        ]],
        onLoadSuccess:function(){
            var rows = $('#grid_txcd').datagrid("getRows");
            if(rows.length>0){
                $('#grid_txcd').datagrid("selectRow", 0);
            }
            txcd.setBtnStatus();
            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        },
        onClickRow:function(){
            txcd.setBtnStatus();
        }
    });
    //**********按钮触发事件**********
   // 导入凭证按钮
    $("#btn_txcd_insert").click(function () {
        txcd_insert();
    });
    //撤销
    $("#btn_txcd_cx").click(function () {
        txcd_cx();
    });
    //重置
    $("#btn_txcd_cz").click(function () {
        txcd_cz();
    });
    //查询按钮
    $("#btn_txcd_search").click(function () {
        txcd_search();
    });
    //取消
    $("#btn_txcd_qx").click(function () {
        txcd_qx();
    });

    //双击事件
//    $("#grid_txcd").datagrid({
//        onDblClickCell:function (index, field) {
//            $('#grid_txcd').datagrid('selectRow',index);
//            var row = $('#grid_txcd').datagrid('getSelected');
//            if (field == "jsdh") {
//                txcd_modify('修改销售单', 'update_txcd');
//            }
//        }
//    });
    //加载查询条件中的申报公司下拉框
    $("#txcd_gs").combotree({
        loadFilter: function (rows) {
            return convert(rows);
        }
    });
});

//贴现承兑撤销
function txcd_cx(){
    var row = $('#grid_txcd').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否撤销该贴现承兑？', function (r) {
            if (r) {
                var index = $('#grid_txcd').datagrid('getRowIndex', row);
                $.ajax({
                    url: $.webPath() + "/cwgl/txcd/revoke_voucher?txcdId="+row.id,
                    dataType: 'json',
                    type: 'POST',
                    beforeSend: function () {
                        $("#layout_txcd").mask();
                    },
                    success:function(msg){
                        $("#layout_txcd").mask("hide");
                        if (msg.responseflag == 0){
                            $('#grid_txcd').datagrid('reload');
                            $.messager.alert("提示", msg.responsemessage, "error");
                        } else {
                            $('#grid_txcd').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: msg.responsemessage,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要撤销的票据！", "warning");
    }
}



//贴现承兑重置
function txcd_cz(){
    var row = $('#grid_txcd').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否重置该票据？', function (r) {
            if (r) {
                var index = $('#grid_txcd').datagrid('getRowIndex', row);
                $.ajax({
                    url: $.webPath() + "/cwgl/txcd/rest_voucher?txcdId="+row.id,
                    dataType: 'json',
                    type: 'POST',
                    beforeSend: function () {
                        $("#layout_txcd").mask();
                    },
                    success:function(msg){
                        $("#layout_txcd").mask("hide");
                        if (msg.responseflag == 0){
                            $('#grid_txcd').datagrid('reload');
                            $.messager.alert("提示", msg.responsemessage, "error");
                        } else {
                            $('#grid_txcd').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: msg.responsemessage,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要重置的票据！", "warning");
    }
}

//贴现承兑取消
function txcd_qx(){
    var row = $('#grid_txcd').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否取消该票据？', function (r) {
            if (r) {
                var index = $('#grid_txcd').datagrid('getRowIndex', row);
                $.ajax({
                    url: $.webPath() + "/cwgl/txcd/revoke_note?txcdId="+row.id,
                    dataType: 'json',
                    type: 'POST',
                    beforeSend: function () {
                        $("#layout_txcd").mask();
                    },
                    success:function(msg){
                        $("#layout_txcd").mask("hide");
                        if (msg.responseflag == 0){
                            $('#grid_txcd').datagrid('reload');
                            $.messager.alert("提示", msg.responsemessage, "error");
                        }else {
                            $('#grid_txcd').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: msg.responsemessage,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要重置的票据！", "warning");
    }
}

//弹票据贴现窗口
function txcd_insert() {
    var row = $('#grid_txcd').datagrid('getSelected');
    if(row){
        var win = $.window({
            title:"票据贴现承兑",
            width:1000,
            height:300,
            cache:false,
            url:$.webPath() + "/cwgl/txcd/txcd_insert?txcdId="+row.id,
            modal:true,
            buttons:[
                {
                    text:'确认',
                    handler:function () {
                        $(win).mask();
                        $.ajax({
                            url:$.webPath() + "/cwgl/txcd/generate_voucher",
                            dataType: 'json',
                            type: 'POST',
                            data:{"txcdSystemDate":$("#txcd_systemDate").datebox("getValue"),
                                "attachment":$("#txcd_attachment").val(),
                                "bz":$("#txcd_bz").val(),
                            "txcdId":$("#txcd_id").val()},
                            beforeSend: function () {
                                if($("#txcd_attachment").val()=="" || $("#txcd_attachment").val()==null){
                                    $.messager.alert("提示","请填写附单据数", "error");
                                    $(win).mask('hide');
                                    return false;
                                }
                                if($("#txcd_systemDate").datebox("getValue")=="" || $("#txcd_systemDate").datebox("getValue")==null){
                                    $.messager.alert("提示","请填写制单时间", "error");
                                    $(win).mask('hide');
                                    return false;
                                }
                            },
                            success:function(msg){
                                $(win).mask('hide');
                                $(win).dialog('close');
                                $("#layout_txcd").mask("hide");
                                if (msg.responseflag == 0){
                                    $('#grid_txcd').datagrid('reload');
                                    $.messager.alert("提示", msg.responsemessage, "error");
                                }else {
                                    $('#grid_txcd').datagrid('reload');
                                    $.messager.show({
                                        title: '提示',
                                        msg: msg.responsemessage,
                                        timeout: 3000,
                                        showType: 'slide'
                                    });
                                }
                            }
                        });
                    }
                }
            ]
        });
    }else{
        $.messager.alert('提示', '请选择一行数据', 'error');
    }
}

//查询销售单结算
function txcd_search() {
    var gsId = $('#txcd_gs').combotree('getValue') == null ? null : $('#txcd_gs').combotree('getValue');
    var gsMc = $('#txcd_gs').combotree('getText') == null ? null : $('#txcd_gs').combotree('getText');
    var txcdZt = $('#txcd_zt').combobox('getValue') == null ? null : $('#txcd_zt').combobox('getValue');
    var txcdPjlx = $('#txcd_pjlx').combobox('getValue') == null ? null : $('#txcd_pjlx').combobox('getValue');
   $('#grid_txcd').datagrid('load',
        {
            jsdh:$('#txcd_jsdh').val(),
            gsId:gsId,
            gsMc:gsMc,
            txcdZt:txcdZt,
            voucherNum:$("#txcd_pzh").val(),
            djh:$("#txcd_djh").val(),
            czr:$("#txcd_czr").val(),
            pjhm:$("#txcd_pjhm").val(),
            txcdPjlx:txcdPjlx,
            rzyh:$("#txcd_rzyh").val()
        }
    );
}
